package com.example.xianglilai_end.Utils;

import com.example.xianglilai_end.Bean.BatteryArea;
import com.example.xianglilai_end.Bean.CityRange;

import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetCityRange {
    //读取城市的polygon
    public static List<CityRange> csvRead() throws IOException {
        List<CityRange> list = new ArrayList<>();
        BufferedReader br = null;
        String line = null;
        String path = "E:\\yangzhiyun\\ok_geo.csv";
        int colum1 = 2,colum2 = 4,colum3 = 6;
        br = new BufferedReader(new FileReader(path));
        br.readLine();
        int i = 0;
        while((line = br.readLine()) != null){
            CityRange temp = new CityRange();
            String deep = line.split(",")[colum1];
            String value1,value2="";
            if(deep.equals("1")){
                value1 = line.split(",")[colum2];
                value2 = line.split(",",7)[colum3];
//                for (int k = colum3;k<line.split(",").length;k++){
//                    value2 = value2+","+line.split(",")[k];
//                }
                String result1[] = value1.split(" ");
                //System.out.println(result1[0]);
                if(result1[0].equals("\"台湾省"))
                    continue;
                value2 = value2.replace(";",",");
                String result2[] = value2.split(",");

                //System.out.println(result1[1]+line.split(",").length+"  " +"  "+result2.length);

                List<double[]> plist = new ArrayList<>();

                for(int j = 0;j<result2.length;j++){
                    result2[j] = result2[j].replace("\"","");
                    String polytemp[] = result2[j].split(" ");
                    double [] polygon = new double[2];
                    if(polytemp.length == 1)
                        continue;
                    for(int k = 0;k<2;k++){
                        polygon[k] = Double.parseDouble(polytemp[k]);
                    }
                    if(i == 1){
                        //System.out.println(polygon[0]+"  "+polygon[1]);
                    }
                    plist.add(polygon);
                }
                temp.setPolygon(plist);
                temp.setProvince(result1[0]);
                temp.setCity(result1[1]);
                list.add(temp);
                i++;
            }
        }
        return list;
    }
    public static Path2D.Double create(CityRange area){
        Path2D.Double generalPath = new Path2D.Double();

        //获取第一个起点经纬度的坐标

        double[] first = area.polygon.get(0);

        //通过移动到以double精度指定的指定坐标，把第一个起点添加到路径中
        //每一个polygon第一个是经度，第二个是纬度

        generalPath.moveTo(first[0], first[1]);

        //把集合中的第一个点删除防止重复添加

        area.polygon.remove(0);

        //循环集合里剩下的所有经纬度坐标

        for (int i = 1;i<area.polygon.size();i++) {

            //通过从当前坐标绘制直线到以double精度指定的新指定坐标，将路径添加到路径。

            //从第一个点开始，不断往后绘制经纬度点

            generalPath.lineTo(area.polygon.get(i)[0], area.polygon.get(i)[1]);

        }

        // 最后要多边形进行封闭，起点及终点

        generalPath.lineTo(first[0], first[1]);

        //将直线绘制回最后一个 moveTo的坐标来关闭当前子路径。

        generalPath.closePath();

        return generalPath;
    }

    public static boolean isPoint(CityRange area, double longitude, double latitude) {

        //JSONArray jsonArray = JSON.parseArray(polygon);
        //
        //将json转换成对象
        //
        //List<ScopeRequest> list = JSON.parseArray(jsonArray.toJSONString(), ScopeRequest.class);

        Path2D path2D = create(area);

        //true如果指定的坐标在Shape边界内; 否则为false。
        Point2D.Double point = new Point2D.Double(longitude,latitude);
//        System.out.println(point);
//        System.out.println(point.x+"   "+point.y);
//        System.out.println(path2D.contains(point));

        return path2D.contains(longitude,latitude);
    }


    //s[0]是纬度,s[1]是经度
    public static BatteryArea GetLocation(double []s) throws IOException {
        BatteryArea batteryArea = new BatteryArea();
        List<CityRange> alist = csvRead();
        int k = 0;
        for(int i = 0;i<alist.size();i++)
        {
            k = i;
            //System.out.println(alist.get(i).province+ " "+alist.get(i).city);
            if(GetCityRange.isPoint(alist.get(i),s[0],s[1]))
                break;
        }
        batteryArea.province = alist.get(k).getProvince();
        batteryArea.city = alist.get(k).getCity();
        return batteryArea;
    }
}
